<template>
  <div class="query-form">
    <el-form ref="form" :inline="true" :model="formData">
      <el-form-item v-for="item in formItems" :key="item.prop" :label="item.label" :prop="item.prop">
        <el-input v-if="item.type === 'input'" v-model="formData[item.prop]" :placeholder="item.placeholder" />
        <el-select style="width: 200px;" v-if="item.type === 'select'" v-model="formData[item.prop]" value-key="value"
          :placeholder="item.placeholder">
          <el-option v-for="option in item.options" :key="option.value" :value="option.value"
            :label="option.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'QueryForm',
  props: {
    // 表单项配置
    formItems: {
      type: Array,
      required: true,
      default: () => []
    },
    // 表单数据
    modelValue: {
      type: Object,
      required: true
    }
  },
  emits: ['update:modelValue', 'query', 'reset'],
  computed: {
    formData: {
      get() {
        return this.modelValue
      },
      set(value) {
        this.$emit('update:modelValue', value)
      }
    }
  },
  methods: {
    // 查询
    handleQuery() {
      this.$emit('query', this.formData)
    },
    // 重置
    handleReset() {
      this.$refs.form.resetFields()
      this.$emit('reset')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>